package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.SalaryBean;
import com.attendance.www.hr.beans.SalaryListBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IClosedService;
import com.attendance.www.hr.service.ISalaryService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 工资报表
 * 2018-04-07
 * @author 张鑫
 *
 */
@Controller
@RequestMapping("/salary")
public class SalaryController {
	
	@Resource
	private ISalaryService salaryService;
	@Resource
	private IClosedService closedService;
	
	/**
	 * 查询提交历史(下拉框)
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getSalaryHistory.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getSalaryHistory(HttpServletRequest request) {
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId=userEmployee.getEmployeeId();
		return ResultMap.success(salaryService.getSalaryHistory(employeeId));
	}
	
	/**
	 * 查询提交明细
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getSalaryListDetail.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getSalaryListDetail(HttpServletRequest request) {
		String salaryRequestIdStr=request.getParameter("salaryRequestId");
		if (StringUtils.isBlank(salaryRequestIdStr)) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(salaryService.getSalaryListDetail(Integer.parseInt(salaryRequestIdStr)));
	}
	/**
	 * 查询工资报表 
	 * 新查询或查看历史数据
	 * @param request
	 * @param salaryBean 必要参数，提交人编号，开始时间，结束时间
	 * @return
	 */
	@RequestMapping(value = "/getSalaryTempList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getSalaryTempList(HttpServletRequest request,@RequestBody SalaryBean salaryBean) {
		if (salaryBean.getPageCount()==null
				||salaryBean.getPageNo()==null
				||(salaryBean.getStartDate()==null && salaryBean.getHistoryStartDate()==null)
				||(salaryBean.getEndDate()==null && salaryBean.getHistoryEndDate()==null)) {
				throw new ResultFaultException("0_00001");
		}
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId=userEmployee.getEmployeeId();
		salaryBean.setEmployeeId(employeeId);
		Integer startNum = 0;
		Integer pageNo = salaryBean.getPageNo();
		Integer pageCount = salaryBean.getPageCount();
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount; 
		}
		salaryBean.setStartNum(startNum);
		return ResultMap.success(salaryService.getSalaryTempList(salaryBean));
	}
	
	/**
	 * 保存修改工资报表(对应“保存”按钮)
	 * @param request
	 * @param salaryBeanList
	 * @return
	 */
	@RequestMapping(value = "/updateSalaryList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object updateSalaryList(HttpServletRequest request,@RequestBody SalaryListBean salaryListBean) {
		if (salaryListBean.getSalaryBeanList().size()==0
				||(salaryListBean.getStartDate()==null && salaryListBean.getHistoryStartDate()==null)
				||(salaryListBean.getEndDate()==null && salaryListBean.getHistoryEndDate()==null)) {
				throw new ResultFaultException("0_00001");
		}
		//获取登陆用户数据
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId=userEmployee.getEmployeeId();
		salaryListBean.setEmployeeId(employeeId);
		if (salaryService.updateSalaryList(salaryListBean)) {
			return ResultMap.success();
		}
		/*if (!closedService.verifyClosed(salaryListBean.getSalaryYear(),salaryListBean.getSalaryMonth())) {//封账验证
		} else {
			throw new ResultFaultException("4_00003");
		}*/
		throw new ResultFaultException("16_00001");
	}
	
	/**
	 * 导入工资报表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/saveSalaryListExcel.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object saveSalaryListExcel(HttpServletRequest request){
		//获取登陆用户数据
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId=userEmployee.getEmployeeId();
		String attachmentId = request.getParameter("attachmentId");
		if (StringUtils.isBlank(attachmentId)) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(salaryService.saveSalaryListExcel(Integer.parseInt(attachmentId), employeeId));
	}

	/**
	 * 导出工资报表
	 * @param request
	 * @param salaryBean
	 * @return
	 */
	@RequestMapping(value = "/getExportSalary.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getExportSalary(HttpServletRequest request,@RequestBody SalaryBean salaryBean) {
		if ((salaryBean.getStartDate()==null && salaryBean.getHistoryStartDate()==null)
				||(salaryBean.getEndDate()==null && salaryBean.getHistoryEndDate()==null)) {
				throw new ResultFaultException("0_00001");
		}
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId=userEmployee.getEmployeeId();
		salaryBean.setEmployeeId(employeeId);
		return ResultMap.success(salaryService.getExportSalary(salaryBean));
	}
	
	/**
	 * 审批人下载提交的报表明细
	 * @param request
	 * @param salaryBean
	 * @return
	 */
	@RequestMapping(value = "/getDownloadSalary.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getDownloadSalary(HttpServletRequest request,@RequestBody SalaryBean salaryBean) {
		if (salaryBean.getSalaryRequestId()==null 
				&& (salaryBean.getSalaryYear()==null || salaryBean.getSalaryMonth()==null)) {
			throw new ResultFaultException("0_00001");
		}
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		salaryBean.setType(1);
		//汇总表下载，by年月
		if (salaryBean.getSalaryYear()!=null && salaryBean.getSalaryMonth()!=null
				&& salaryBean.getSalaryRequestId()==null) {
			salaryBean.setType(null);
		}
		return ResultMap.success(salaryService.getExportSalary(salaryBean));
	}
	
	/**
	 * 获取excel年月
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getExcelYearMonth.do", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Object getExcelYearMonth(HttpServletRequest request) {
		//获取登陆用户数据
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId=userEmployee.getEmployeeId();
		String attachmentId = request.getParameter("attachmentId");
		if (StringUtils.isBlank(attachmentId)) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(salaryService.getExcelYearMonth(Integer.parseInt(attachmentId), employeeId));
	}
	
	/**
	 * 删除工资报表
	 * 根据开始日期，结束日期，人员no
	 * @param request
	 * @param salaryBean
	 * @return
	 */
	@RequestMapping(value = "/deleteSalary.do", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Object deleteSalary(HttpServletRequest request,@RequestBody SalaryBean salaryBean) {
		if (salaryBean.getHistoryStartDate()==null || salaryBean.getHistoryEndDate()==null) {
			throw new ResultFaultException("0_00001");
		}
		//获取登陆用户数据
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId=userEmployee.getEmployeeId();
		salaryBean.setEmployeeId(employeeId);
		return ResultMap.success(salaryService.deleteSalary(salaryBean));
	}
	
	/**
	 * 查询工资汇总表 by年份，月份
	 * @param request
	 * @param salaryBean
	 * @return
	 */
	@RequestMapping(value = "/getSalaryTotalList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getSalaryTotalList(HttpServletRequest request,@RequestBody SalaryBean salaryBean) {
		if (salaryBean.getPageCount()==null
				||salaryBean.getPageNo()==null
				||salaryBean.getSalaryYear()==null
				||salaryBean.getSalaryMonth()==null) {
				throw new ResultFaultException("0_00001");
		}
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer startNum = 0;
		Integer pageNo = salaryBean.getPageNo();
		Integer pageCount = salaryBean.getPageCount();
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount; 
		}
		salaryBean.setStartNum(startNum);
		return ResultMap.success(salaryService.getSalaryTotalList(salaryBean));
	}
}
